import { useRouter, useRoute } from 'vue-router'
import {ref,computed,onMounted} from 'vue'
import type {IHospitalUser } from '@/typings/HospitalType'
import type { Ref } from 'vue'
import { objToQuery } from '@/utils/tools'
import {getHospitalUserApi} from '@/api/hospitalApi'
type HospitalUserList = [
    Ref<number>,
    Ref<IHospitalUser[]>,
    Ref<number>,
    Ref<number>,
    (page:number,size:number,formData:Record<string,string>|undefined) => Promise<void>,
]

const useHospitalUserList = ():HospitalUserList => {
    const router=useRouter()
    const route =useRoute()

    const data=ref<IHospitalUser[]>([])
    const total=ref(0)

    const currentPage1=computed(()=>{
        return route.query.currentPage?+route.query.currentPage:1
    })
    const pageSize1=computed(()=>{
        return route.query.pageSize ? +route.query.pageSize:10
    })

    const loadData=async(currentPage:number,pageSize:number,formdata?:Record<string,string>|undefined)=>{
        router.replace(`?currentPage=${currentPage}&pageSize=${pageSize}`)
        let query=''
        if(typeof formdata !='undefined'){
            query=objToQuery(formdata)
        }
        var ret = await getHospitalUserApi(currentPage1.value, pageSize1.value)
    
    if (ret.code === 0) {
        data.value = ret.data.usersdata
        total.value = ret.data.total
    }

    }
    onMounted(()=>{
        loadData(currentPage1.value, pageSize1.value)
    })
    return [currentPage1,data,total,pageSize1,loadData]
}
export default useHospitalUserList